package com.szholly.pro.watergas_server.common.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.RedirectUrlBuilder;

@SuppressWarnings("deprecation")
public class MyLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
	
	public void commence(HttpServletRequest request,  
            HttpServletResponse response, AuthenticationException authException)  
            throws IOException, ServletException {  
        String returnUrl = buildHttpReturnUrlForRequest(request);  
        System.out.println("【设置returnUrl】:"+returnUrl);
        request.getSession().setAttribute("preUrl", returnUrl);  
        super.commence(request, response, authException);  
    } 
	
	 protected String buildHttpReturnUrlForRequest(HttpServletRequest request)  
	            throws IOException, ServletException {  
	  
	  
	        RedirectUrlBuilder urlBuilder = new RedirectUrlBuilder();  
	        urlBuilder.setScheme("http");  
	        urlBuilder.setServerName(request.getServerName());  
	        urlBuilder.setPort(request.getServerPort());  
	        urlBuilder.setContextPath(request.getContextPath());  
	        urlBuilder.setServletPath(request.getServletPath());  
	        urlBuilder.setPathInfo(request.getPathInfo());  
	        urlBuilder.setQuery(request.getQueryString());  
	  
	        return urlBuilder.getUrl();  
	 }
}
